package com.bluesky.plum.uimodels.standard.components;

import com.bluesky.plum.richdomain.RichDomainField;
import com.bluesky.plum.uimodels.standard.FieldUIComponent;
import com.bluesky.plum.uimodels.standard.StandardUIComponentType;
import com.bluesky.plum.uimodels.standard.UIComponentBuilder;
import com.bluesky.plum.uimodels.standard.components.containers.Panel;
import com.bluesky.plum.uimodels.standard.components.containers.form.commonForms.EntityEditDialogBuilder;
import com.bluesky.plum.uimodels.standard.components.containers.window.Dialog;
import com.bluesky.plum.uimodels.standard.components.containers.window.Window;
import com.bluesky.plum.uimodels.standard.components.objectBrowser.collectionEditor.CollectionEditDialogBuilder;

/**
 * If the parameter is a Object and a new one. We have to provide a Object
 * editor, prefer a modal dialog, but not limited. <br>
 * Basically it shows the Object's all properties in a form.
 * 
 * <br>
 * if the property is a entity,
 * 
 * @author HZ00260
 * 
 */
public class ObjectEditor extends FieldUIComponent {

	protected Object entity;

	protected Panel box;
	protected Button btn;

	public ObjectEditor(RichDomainField field) {
		super(field);
		entity = field.value;

		try {
			if (field.value == null)
				entity = field.fieldClass.newInstance();
			else
				entity = field.value;

		} catch (Exception e) {
			throw new RuntimeException(e);
		}

	}

	@Override
	public void setUiComponentBuilder(UIComponentBuilder uiComponentBuilder) {

		super.setUiComponentBuilder(uiComponentBuilder);

		box = (Panel) uiComponentBuilder
				.createNonFieldComponent(StandardUIComponentType.Panel);

		btn = (Button) uiComponentBuilder
				.createNonFieldComponent(StandardUIComponentType.Button);

		btn.setCaption("...");

		btn.setClickListener(new EventListener() {
			@Override
			public void process() {

				final Window window = getWindow();

				EntityEditDialogBuilder dialogBuilder = new EntityEditDialogBuilder(
						entity, getUiComponentBuilder());

				Dialog dialog = dialogBuilder.getDialog(window,
						"entity edit dialog");

				window.getRootWindow().repaint();

				dialog.setModal(true);
				dialog.pack();

				// it is a modal dialog, so the code will not proceed until the
				// dialog is closed
				dialog.setVisible(true);

				// remove it
				window.removeChildWindow(dialog);

				System.out.println("exit from object edit dialog");

				// after the dialog closed, we can get the entity if we want, by
				// default, the input parameter entity is updated
				// dialogBuilder.getEntity();

			}
		});

		box.addChildComponent(btn);

		addChildComponent(box);
	}

	public Object getEntity() {

		return entity;

	}

	public void setEntity(Object entity) {
		this.entity = entity;
	}

	@Override
	public Object getDomainFieldValue() {

		// get the value from the target UI component.

		return getEntity();

	}

	@Override
	public void setDomainFieldValue(Object value) {

		setEntity(value);
	}

	@Override
	public Object getNativeComponent() {

		return box.getNativeComponent();
	}
}
